package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

public class Menu {

    private Map<Integer, String> options = new HashMap<Integer, String>();
    BufferedReader reader;
    String currentUser;
    boolean isloggedIn;
    UserAccount userAccount;
    boolean hasQuit;
    private java.io.PrintStream stream;
    public final static String WELCOME_MESSAGE = "Welcome to the Bangalore library\n";

    public Menu(PrintStream stream) {
        this.stream = stream;
        options.put(1, "View available books");
        options.put(2, "Reserve a book");
        options.put(3, "Check Membership Status");
        options.put(4, "View movies");
        options.put(5,"Quit");
        reader = new BufferedReader(new InputStreamReader(System.in));
        isloggedIn = false;
        userAccount = new UserAccount(stream);
        hasQuit = false;
    stream.println(WELCOME_MESSAGE);
    }

    public void displayMenuItems() {
        stream.println(options);
    }

    public void selectMenuItem(int optionSelected) throws IOException {
        validateMenuChoice(optionSelected);
        stream.println(options.get(optionSelected));
        executeOptionSelected(optionSelected);
    }

    private void executeOptionSelected(int optionSelected) throws IOException {
        Library library = new Library(stream);
        Movie movie = new Movie(stream);
        if (optionSelected == 1) {
            library.displayBooksAvailable();
        } else if (optionSelected == 2) {
            reserveBook(library);
        } else if (optionSelected == 3) {
            viewMembershipDetails();
        } else if (optionSelected == 4) {
            displayMovies(movie);
        }else if(optionSelected == 5){
            hasQuit = true;
        }
    }

    public boolean quitProgram() {
        return !hasQuit;
    }

    private void displayMovies(Movie movie) throws IOException {
        provideLoginDetails();
        movie.displayMovies();
    }

    private void viewMembershipDetails() throws IOException {
        provideLoginDetails();
        displayUserDetails();
    }

    private void reserveBook(Library library) throws IOException {
        provideLoginDetails();
        library.displayBooksAvailable();
        library.reserveBook();
    }

    public void provideLoginDetails() throws IOException {
        if (isloggedIn == false) {
            stream.println("Please enter your username");
           String userName = reader.readLine();
            stream.println("Please enter your password");
            String password = reader.readLine();
            authenticateUser(userName,password);
        }

    }

    private void authenticateUser(String userName, String password) throws IllegalArgumentException {
        if (isloggedIn = userAccount.checkLoginDetails(userName, password)) {
            currentUser = userName;
        }
        else{
            throw new IllegalArgumentException("You are currently not logged in,Please provide you details before you can proceed");
        }
    }


    private void validateMenuChoice(int optionSelected) {
        if (optionSelected < 1 || optionSelected > 5) {
            throw new IllegalArgumentException("Select a valid option!!");
        }
    }
    public void displayUserDetails(){
        if(isloggedIn){
            userAccount.checkMembership(currentUser);
        }
        else{
            stream.println("Please talk to a Librarian. Thank you.");
        }

    }




}
